define([
    'dojo/_base/declare',
    'dijit/_WidgetBase',
    'dijit/_TemplatedMixin',
    'dijit/_WidgetsInTemplateMixin',
    'dojo/_base/lang',
    'dojo/dom-construct',
    'dojo/topic',
    'dojo/aspect'
], function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, lang, domConstruct, topic, aspect) {

    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
        templateString: '<div><div style="text-align:center;"><button data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick:toggleEditing" data-dojo-props="label:\'Start Editing\',class:\'success\'" data-dojo-attach-point="toggleBTN"></button></div><div class="editDijit" style="margin-top:5px;" data-dojo-attach-point="containerNode"></div></div>',
        widgetsInTemplate: true,
        editor: null,
        isEdit: false,
        mapClickMode: null,
        postCreate: function () {
            this.inherited(arguments);
            this.own(topic.subscribe('mapClickMode/currentSet', lang.hitch(this, 'setMapClickMode')));
            if (this.parentWidget && this.parentWidget.toggleable) {
                this.own(aspect.after(this.parentWidget, 'toggle', lang.hitch(this, function () {
                    this.onLayoutChange(this.parentWidget.open);
                })));
            }
        },
        toggleEditing: function () {
            if (!this.isEdit) {
                var ops = lang.clone(this.settings);
                ops.map = this.map;
                ops.layerInfos = this.layerInfos;

                var con = domConstruct.create('div', {
                    innerHTML: '<img style="display:inline;" src=""/>',
                    'style': 'text-align:center;'
                }, this.containerNode, 'only');

                require(['esri/dijit/editing/Editor'], lang.hitch(this, function (Editor) {
                    this.editor = new Editor({
                        settings: ops
                    }, con);
                    this.editor.startup();
                }));

                this.toggleBTN.set('label', 'Stop Editing');
                this.toggleBTN.set('class', 'danger');
                this.isEdit = true;
                topic.publish('mapClickMode/setCurrent', 'editor');
            } else {
                this.endEditing();
                topic.publish('mapClickMode/setDefault');
            }
        },
        endEditing: function () {
            if (this.editor && this.editor.destroyRecursive) {
                this.editor.destroyRecursive();
            }
            this.toggleBTN.set('label', 'Start Editing');
            this.toggleBTN.set('class', 'success');
            this.isEdit = false;
            this.editor = null;
        },

        onLayoutChange: function (open) {
            // end edit on close of title pane
            if (!open && this.mapClickMode === 'editor') {
                this.endEditing();
                topic.publish('mapClickMode/setDefault');
            }
        },
        setMapClickMode: function (mode) {
            this.mapClickMode = mode;
            if (mode !== 'editor') {
                this.endEditing();
            }
        }
    });
});